<template>
  <view class="my-settle-container">
    <label class="radio" @click="changeAllState">
      <radio color="#C00000" :checked="isFullCheck" /><text>全选</text>
    </label>
  
    <view class="amount-box">
      合计:<text class="amount">￥{{cartList.checkedGoodsAmount}}</text>
    </view>
  
    <view class="btn-settle" @click="settlement">结算({{cartList.checkCount}})</view>
  </view>
</template>

<script setup>
import { computed, ref } from "vue";
import {moduleCart} from '../../store/cart.js'
import {moduleUser} from '../../store/user.js'
  const seconds=ref(3)
  const timer=ref(null)
  const cartList=moduleCart()
  const userList=moduleUser()
  const isFullCheck=computed(()=>{
    return cartList.total===cartList.checkCount
  })
  
  const changeAllState=()=>{
    const flag=cartList.total===cartList.checkCount
    cartList.updateAllGoodsState(!flag)
  }
  const settlement=()=>{
    if (!cartList.checkCount) return uni.$showMsg('请选择要结算的商品！')
    if (!userList.addstr) return uni.$showMsg('请选择收货地址！')
    if (!userList.token) return delayNavigate()
    payOrder()
  }
  const showTips=(n)=>{
    uni.showToast({
      icon:"none",
      title:'请登录后再结算！' + n + ' 秒后自动跳转到登录页',
      mask:true,
      duration:1500
    })
  }
  const delayNavigate=()=>{
    seconds.value=3
    showTips(seconds.value)
    timer.value= setInterval(()=>{
      seconds.value--
      if(seconds.value<=0){
        clearInterval(timer.value)
        uni.switchTab({
          url:'/pages/my/my',
          success:()=>{
            userList.updateRedirectInfo({
              openType:'switchTab',
              from:'/pages/cart/cart'
            })
          }
        })
         return
      }
      showTips(seconds.value)
    },1000)
  }
  const payOrder=async()=>{
    const orderInfo={
      order_price:0.01,
      consignee_addr: userList.addstr,
      goods:cartList.cart.filter(x => x.goods_state).map(x => ({ goods_id: x.goods_id, goods_number: x.goods_count, goods_price: x.goods_price }))
  }
     const { data: res } = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
      if (res.meta.status !== 200) return uni.$showMsg('创建订单失败！')
      const orderNumber = res.message.order_number
     
     // const { data: res2 } = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', { order_number: orderNumber })
     // console.log(res2)
       // if (res2.meta.status !== 200) return uni.$showError('预付订单生成失败！')
       // const payInfo = res2.message.pay
       const payInfo ={
              "timeStamp": "1525681145",
               "nonceStr": "BkPggorBXZwPGXe3",
               "package": "prepay_id=wx071619042918087bb4c1d3d72999385683",
               "signType": "MD5",
               "paySign": "D1642DEEF1663C8012EDEB9297E1D516"
       }
       
       
          // const [err, succ] = await uni.requestPayment(payInfo)
          // if (err) return uni.$showMsg('订单未支付！')
          // const { data: res3 } = await uni.$http.post('/api/public/v1/my/orders/chkOrder', { order_number: orderNumber })
          // console.log(res3)
          // if (res3.meta.status !== 200) return uni.$showMsg('订单未支付！')
          uni.showToast({
            title: '支付完成！',
            icon: 'success'
          })
      
    }
  
</script>

<style lang="scss">
.my-settle-container {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 50px;
  // 将背景色从 cyan 改为 white
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 5px;
  font-size: 14px;

  .radio {
    display: flex;
    align-items: center;
  }

  .amount {
    color: #c00000;
  }

  .btn-settle {
    height: 50px;
    min-width: 100px;
    background-color: #c00000;
    color: white;
    line-height: 50px;
    text-align: center;
    padding: 0 10px;
  }
}
</style>